Skip to main content

Trim MP4 Beginning

Description

The trim_mp4_beginning function trims the beginning of an MP4 video by a specified number of seconds. It uses ffmpeg to process the video and saves the trimmed result to a new file.

Function Signature:

def trim_mp4_beginning(file_path: str, seconds_to_trim: int, new_file_path: str) -> int:

Parameters

  • file_path (str): The path to the original MP4 video file.
  • seconds_to_trim (int): The number of seconds to trim from the beginning of the video.
  • new_file_path (str): The path where the trimmed video will be saved.

Returns

  • int: The function returns 0 if the trimming is successful, or -1 if an error occurs.

Example Usage

input_file = "/path/to/video.mp4"
output_file = "/path/to/trimmed_video.mp4"
seconds_to_trim = 10 # Trim the first 10 seconds

result = trim_mp4_beginning(input_file, seconds_to_trim, output_file)

if result == 0:
print("Video trimmed successfully.")
else:
print("Error occurred during video trimming.")

Notes

  • The function uses ffmpeg with the -ss option to specify the start time for the trimmed video.
  • The video is copied without re-encoding (-c copy) to preserve quality and speed up the process.
  • The trimming process is done by specifying the start time in milliseconds.

Error Handling

  • If any error occurs (e.g., invalid file paths, ffmpeg errors), the function will print the error message and return -1.